<?php 


require "../common/Curl.php";

/**
 *	https://open.esign.cn/doc/detail?id=opendoc%2Fidentity_service%2Feaug1b&namespace=opendoc%2Fidentity_service
 * 	e签宝 认证服务API
 * 	企业认证纯API版----接口顺序排列封装
 *  使用请求签名鉴权方式
 *  下一个------------------ 信息对比 content.php
 */


//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * [createOrganAuthName 发起企业实名认证4要素校验]
 * @param  [type] $accountId      [组织机构账号ID]
 * @param  [type] $agentAccountId [办理人账号Id]
 * @param  [type] $repetition     [是否允许账号重复实名。默认允许重复实名]
 * @param  [type] $contextId      [对接方业务上下文id]
 * @param  [type] $notifyUrl      [认证结束后异步通知地址]
 * @param  [type] $frAuthEnable   [是否需要进行法定代表人认证判断]
 * @return [type]                 [description]
 */
function createOrganAuthName($accountId,$agentAccountId,$repetition,$contextId,$notifyUrl,$frAuthEnable,$appId,$secret,$url){
	$urlss = $url."/v2/identity/auth/api/organization/enterprise/".$accountId."/fourFactors";
	$head_url = "/v2/identity/auth/api/organization/enterprise/".$accountId."/fourFactors";
    $data = [
		"agentAccountId"=>$agentAccountId,
		"repetition"=>$repetition,
	    "frAuthEnable"=>$frAuthEnable,
	    "contextId"=>$contextId,
	    "notifyUrl"=>$notifyUrl
	];
    $res = json_encode($data);
    $header = getHeader($data,"POST",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="POST",$urlss,$header,$res);
    return $result;
} 
// $accountId = "6f69dc0a5c8d415aa6a0c7e239a32241";
// $agentAccountId = "f120cf3f2a124fc88f8438d6c919b8b6";
// $repetition = true;
// $contextId = "";
// $notifyUrl = "";
// $frAuthEnable = "";
// $a = createOrganAuthName($accountId,$agentAccountId,$repetition,$contextId,$notifyUrl,$frAuthEnable,$appId,$secret,$url);
// var_dump($a);die;


//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


/**
 * [createAuthBody 发起企业核身认证4要素校验]
 * @param  [type] $name             [组织机构证件]
 * @param  [type] $orgCode          [组织机构证件号]
 * @param  [type] $legalRepCertType [法人证件类型]
 * @param  [type] $legalRepName     [法定代表人名称]
 * @param  [type] $legalRepIdNo     [法定代表人证件号]
 * @param  [type] $contextId        [对接方业务上下文id]
 * @param  [type] $notifyUrl        [认证结束后异步通知地址]
 * @return [type]                   [description]
 */
function createAuthBody($name,$orgCode,$legalRepCertType,$legalRepName,$legalRepIdNo,$contextId,$notifyUrl,$appId,$secret,$url){
	$urlss = $url."/v2/identity/auth/api/organization/enterprise/fourFactors";
	$head_url = "/v2/identity/auth/api/organization/enterprise/fourFactors";
    $data = [
    	"name"=>$name,
		"orgCode"=>$orgCode,
		"legalRepCertType"=>$legalRepCertType,
	    "legalRepName"=>$legalRepName,
	    "legalRepIdNo"=>$legalRepIdNo,
	    "contextId"=>$contextId,
	    "notifyUrl"=>$notifyUrl
	];
    $res = json_encode($data);
    $header = getHeader($data,"POST",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="POST",$urlss,$header,$res);
    return $result;
} 
// $name = "赣州刘颂晶网络";
// $orgCode = "546512395648525649";
// $legalRepCertType = "INDIVIDUAL_CH_IDCARD";
// $legalRepName = "liusongjing";
// $legalRepIdNo = "360731199907050379";
// $contextId = "f0a7927d-c5f9-4652-a053-1130d86c8fa8";
// $notifyUrl = "http://172.20.62.10:8080/testnotify/msgRecive";
// $a = createAuthBody($name,$orgCode,$legalRepCertType,$legalRepName,$legalRepIdNo,$contextId,$notifyUrl,$appId,$secret,$url);
// var_dump($a);die;

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * [createOrganAuthNameThree 发起企业实名认证3要素校验]
 * @param  [type] $accountId      [组织机构账号ID]
 * @param  [type] $agentAccountId [办理人账号Id]
 * @param  [type] $repetition     [是否允许账号重复实名]
 * @param  [type] $contextId      [对接方业务上下文id]
 * @param  [type] $notifyUrl      [认证结束后异步通知地址]
 * @return [type]                 [description]
 */
function createOrganAuthNameThree($accountId,$agentAccountId,$repetition,$contextId,$notifyUrl,$appId,$secret,$url){
	$urlss = $url."/v2/identity/auth/api/organization/".$accountId."/threeFactors";
	$head_url = "/v2/identity/auth/api/organization/".$accountId."/threeFactors";
    $data = [
		"agentAccountId"=>$agentAccountId,
		"repetition"=>$repetition,
	    "contextId"=>$contextId,
	    "notifyUrl"=>$notifyUrl
	];
    $res = json_encode($data);
    $header = getHeader($data,"POST",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="POST",$urlss,$header,$res);
    return $result;
} 
// $accountId = "6f69dc0a5c8d415aa6a0c7e239a32241";
// $agentAccountId = "f120cf3f2a124fc88f8438d6c919b8b6";
// $repetition = true;
// $contextId = "";
// $notifyUrl = "";
// $a = createOrganAuthNameThree($accountId,$agentAccountId,$repetition,$contextId,$notifyUrl,$appId,$secret,$url);
// var_dump($a);die;

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * [createAuthBodyThree 发起企业核身认证3要素检验]
 * @param  [type] $name         [组织机构证件（如营业执照）上的组织机构名称]
 * @param  [type] $orgCode      [组织机构证件号,支持统一社会信用代码号和工商注册号]
 * @param  [type] $legalRepName [法定代表人名称]
 * @param  [type] $contextId    [对接方业务上下文id]
 * @param  [type] $notifyUrl    [认证结束后异步通知地址,]
 * @return [type]               [description]
 */
function createAuthBodyThree($name,$orgCode,$legalRepName,$contextId,$notifyUrl,$appId,$secret,$url){
	$urlss = $url."/v2/identity/auth/api/organization/threeFactors";
	$head_url = "/v2/identity/auth/api/organization/threeFactors";
    $data = [
    	"name"=>$name,
		"orgCode"=>$orgCode,
	    "legalRepName"=>$legalRepName,
	    "contextId"=>$contextId,
	    "notifyUrl"=>$notifyUrl
	];
    $res = json_encode($data);
    $header = getHeader($data,"POST",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="POST",$urlss,$header,$res);
    return $result;
} 
// $name = "赣州刘颂晶网络";
// $orgCode = "546512395648525649";
// $legalRepName = "liusongjing";
// $contextId = "f0a7927d-c5f9-4652-a053-1130d86c8fa8";
// $notifyUrl = "http://172.20.62.10:8080/testnotify/msgRecive";
// $a = createAuthBodyThree($name,$orgCode,$legalRepName,$contextId,$notifyUrl,$appId,$secret,$url);
// var_dump($a);die;

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


/**
 * [createGrantAuthName 发起授权签署实名认证]
 * @param  [type] $flowId       [实名认证流程Id]
 * @param  [type] $mobileNo     [法定代表人手机号，用于签署电子授权书]
 * @param  [type] $legalRepIdNo [法定代表人身份证号,如果企业账号中已设置]
 * @param  [type] $redirectUrl  [法定代表人签署完成后，重定向跳转地址]
 * @return [type]               [description]
 */
function createGrantAuthName($flowId,$mobileNo,$legalRepIdNo,$redirectUrl,$appId,$secret,$url){
	$urlss = $url."/v2/identity/auth/api/organization/".$flowId."/legalRepSignAuth";
	$head_url = "/v2/identity/auth/api/organization/".$flowId."/legalRepSignAuth";
    $data = [
		"mobileNo"=>$mobileNo,
	    "legalRepIdNo"=>$legalRepIdNo,
	    "redirectUrl"=>$redirectUrl
	];
    $res = json_encode($data);
    $header = getHeader($data,"POST",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="POST",$urlss,$header,$res);
    return $result;
}
// $flowId = "1941864529655236119";
// $mobileNo = "18888888888";
// $legalRepIdNo = "360731199907050379";
// $redirectUrl = "";
// $a = createGrantAuthName($flowId,$mobileNo,$legalRepIdNo,$redirectUrl,$appId,$secret,$url);
// var_dump($a);die;


//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * [createGrantAuthBody 发起授权签署核身认证]
 * @param  [type] $flowId       [实名认证流程ID]
 * @param  [type] $agentName    [当前实名认证办理人姓名]
 * @param  [type] $agentIdNo    [当前实名认证办理人证件号]
 * @param  [type] $mobileNo     [法定代表人手机号，用于签署电子授权书]
 * @param  [type] $legalRepIdNo [法定代表人身份证号,如果信息比对api中已传入]
 * @param  [type] $redirectUrl  [法定代表人签署完成后，重定向跳转地址]
 * @return [type]               [description]
 */
function createGrantAuthBody($flowId,$agentName,$agentIdNo,$mobileNo,$legalRepIdNo,$redirectUrl,$appId,$secret,$url){
	$urlss = $url."/v2/identity/auth/api/organization/".$flowId."/legalRepSign";
	$head_url = "/v2/identity/auth/api/organization/".$flowId."/legalRepSign";
    $data = [
		"agentName"=>$agentName,
	    "agentIdNo"=>$agentIdNo,
	    "mobileNo"=>$mobileNo,
	    "legalRepIdNo"=>$legalRepIdNo,
	    "redirectUrl"=>$redirectUrl
	];
    $res = json_encode($data);
    $header = getHeader($data,"POST",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="POST",$urlss,$header,$res);
    return $result;
}
// $flowId = "1941864529655236119";
// $agentName = "liusongjing";
// $agentIdNo = "360731199907050379";
// $mobileNo = "15179798856";
// $legalRepIdNo = "360731199907050379";
// $redirectUrl = "";
// $a = createGrantAuthBody($flowId,$agentName,$agentIdNo,$mobileNo,$legalRepIdNo,$redirectUrl,$appId,$secret,$url);
// var_dump($a);die;

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


/**
 * [getAuthurl 获取授权签署链接]
 * @param  [type] $flowId [实名认证流程Id]
 * @return [type]         [description]
 */
function getAuthurl($flowId,$appId,$secret,$url){
	$urlss = $url."/v2/identity/auth/pub/organization/".$flowId."/signUrl";
	$head_url = "/v2/identity/auth/pub/organization/".$flowId."/signUrl";
	$data =[];
    $res = json_encode($data);
    $header = getHeader($data,"GET",$head_url,$appId,$secret);
    $result = sendHttp($method="GET",$urlss,$header,$res);
    return $result;
}
// $flowId = "1941864529655236119";
// $a = getAuthurl($flowId,$appId,$secret,$url);
// var_dump($a);die;

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


/**
 * [getAuthBookStatus 查询授权书签署状态]
 * @param  [type] $flowId [实名认证流程Id]
 * @return [type]         [description]
 */
function getAuthBookStatus($flowId,$appId,$secret,$url){
	$urlss = $url."/v2/identity/auth/pub/organization/".$flowId."/legalRepSignResult";
	$head_url = "/v2/identity/auth/pub/organization/".$flowId."/legalRepSignResult";
	$data =[];
    $res = json_encode($data);
    $header = getHeader($data,"GET",$head_url,$appId,$secret);
    $result = sendHttp($method="GET",$urlss,$header,$res);
    return $result;
}
// $flowId = "1941864529655236119";
// $a = getAuthBookStatus($flowId,$appId,$secret,$url);
// var_dump($a);die;

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * [createMoneyAuth 发起随机金额打款认证]
 * @param  [type] $flowId    [description]
 * @param  [type] $bank      [description]
 * @param  [type] $province  [description]
 * @param  [type] $city      [description]
 * @param  [type] $subbranch [description]
 * @param  [type] $cardNo    [description]
 * @param  [type] $cnapsCode [description]
 * @param  [type] $contextId [description]
 * @param  [type] $notifyUrl [description]
 * @param  [type] $mobile    [description]
 * @return [type]            [description]
 */
function createMoneyAuth($flowId,$bank,$province,$city,$subbranch,$cardNo,$cnapsCode,$contextId,$notifyUrl,$mobile,$appId,$secret,$url){
    $urlss = $url."/v2/identity/auth/pub/organization/".$flowId."/transferRandomAmount";
    $head_url = "/v2/identity/auth/pub/organization/".$flowId."/transferRandomAmount";
    $data = array(
        'flowId'=>$flowId,
        'bank'=>$bank,
        'province'=>$province,
        'city'=>$city,
        'subbranch'=>$subbranch,
        'cardNo'=>$cardNo,
        'cnapsCode'=>$cnapsCode,
        'contextId'=>$contextId,
        'notifyUrl'=>$notifyUrl,
        'mobile'=>$mobile
    );
    $res = json_encode($data);
    $header = getHeader($data,"PUT",$head_url,$appId,$secret);
    $result = sendHttp($method="PUT",$urlss,$header,$res);
    return $result;
}
// $flowId = "1941864529655236119";
// $bank = "平安银行";
// $province = "浙江";
// $city = "杭州市";
// $subbranch = "平安银行杭州高新支行";
// $cardNo = "110034**75702";
// $cnapsCode = "307***2584";
// $contextId = "f0a7927d-c5f9-4652-a053-1130d86c8fa8";
// $notifyUrl = "";
// $mobile = "";
// $a = createMoneyAuth($flowId,$bank,$province,$city,$subbranch,$cardNo,$cnapsCode,$contextId,$notifyUrl,$mobile,$appId,$secret,$url);
// var_dump($a);die;

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * [randomMoneyAuth 随机金额校验]
 * @param  [type] $flowId [实名认证流程Id]
 * @param  [type] $amount [对公账号收到的随机金额数值]
 * @return [type]         [description]
 */
function randomMoneyAuth($flowId,$amount,$appId,$secret,$url){
    $urlss = $url."/v2/identity/auth/pub/organization/".$flowId."/verifyRandomAmount";
    $head_url = "/v2/identity/auth/pub/organization/".$flowId."/verifyRandomAmount";
    $data = array(
        'amount'=>$amount
    );
    $res = json_encode($data);
    $header = getHeader($data,"PUT",$head_url,$appId,$secret);
    $result = sendHttp($method="PUT",$urlss,$header,$res);
    return $result;
}
// $flowId = "1941864529655236119";
// $amount = "0.01";
// $a = randomMoneyAuth($flowId,$amount,$appId,$secret,$url);
// var_dump($a);die;

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * [selectRandomMoneyStatus 查询随机金额打款进度]
 * @param  [type] $flowId [项目id]
 * @return [type]         [description]
 */
function selectRandomMoneyStatus($flowId,$appId,$secret,$url){
	$urlss = $url."/v2/identity/auth/pub/organization/".$flowId."/transferProcess";
	$head_url = "/v2/identity/auth/pub/organization/".$flowId."/transferProcess";
	$data =[];
    $res = json_encode($data);
    $header = getHeader($data,"GET",$head_url,$appId,$secret);
    $result = sendHttp($method="GET",$urlss,$header,$res);
    return $result;
}
// $flowId = "1941864529655236119";
// $a = selectRandomMoneyStatus($flowId,$appId,$secret,$url);
// var_dump($a);die;

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


/**
 * [createOrganAuthReverse 发起企业反向打款认证]
 * @param  [type] $flowId [实名认证流程Id]
 * @return [type]         [description]
 */
function createOrganAuthReverse($flowId,$appId,$secret,$url){
	$urlss = $url."/v2/identity/auth/pub/organization/".$flowId."/reversePayment";
	$head_url = "/v2/identity/auth/pub/organization/".$flowId."/reversePayment";
    $data = [];
    $res = json_encode($data);
    $header = getHeader($data,"POST",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="POST",$urlss,$header,$res);
    return $result;
}
// $flowId = "1941864529655236119";
// $a = createOrganAuthReverse($flowId,$appId,$secret,$url);
// var_dump($a);die;


//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * [selectOrganReverseStatus 查询反向打款进度]
 * @param  [type] $flowId [实名认证流程Id]
 * @return [type]         [description]
 */
function selectOrganReverseStatus($flowId,$appId,$secret,$url){
	$urlss = $url."/v2/identity/auth/pub/organization/".$flowId."/query/reversePaymentProcess";
	$head_url = "/v2/identity/auth/pub/organization/".$flowId."/query/reversePaymentProcess";
	$data =[];
    $res = json_encode($data);
    $header = getHeader($data,"GET",$head_url,$appId,$secret);
    $result = sendHttp($method="GET",$urlss,$header,$res);
    return $result;
}
// $flowId = "1941864529655236119";
// $a = selectOrganReverseStatus($flowId,$appId,$secret,$url);
// var_dump($a);die;

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * [selectBankContent 查询打款银行信息]
 * @param  [type] $flowId  [实名认证流程Id]
 * @param  [type] $keyWord [银行名称搜索关键字，建议输入完整的银行名称]
 * @return [type]          [description]
 */
function selectBankContent($flowId,$keyWord,$appId,$secret,$url){
	$urlss = $url."/v2/identity/auth/pub/organization/".$flowId."/subbranch?keyWord=".$keyWord;
	$head_url = "/v2/identity/auth/pub/organization/".$flowId."/subbranch?keyWord=".$keyWord;
	$data =[];
    $res = json_encode($data);
    $header = getHeader($data,"GET",$head_url,$appId,$secret);
    $result = sendHttp($method="GET",$urlss,$header,$res);
    return $result;
}
// $flowId = "1941864529655236119";
// $keyWord = "平安";
// $a = selectBankContent($flowId,$keyWord,$appId,$secret,$url);
// var_dump($a);die;


